<!-- 地址管理 -->
<template>
	<view class="address-management-page">
		<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback">
			<block v-for="(i, j) in list" :key="j">
				<l-swipeAction :options="options" @clickItem="clickItem($event, i.id)">
					<view class="flex-columns address-card" @click="toDetail(i)">
						<view class="flex-align-center p-bottom20">
							<view class="font32 p-around15">{{i.name}}</view>
							<view class="font32 p-around15">{{i.phone}}</view>
							<!-- defaultNot =“是否默认 1.是 2.否”) -->
							<view class="v-default" v-if="i.status == 1">默认</view>
							<!-- <view class="v-label">{{i.label}}</view> -->
						</view>
						<view class="flex-top-start address-bottom">
							<view class="font-overflow flex1 ">{{ i.province_name }} {{ i.city_name }}
								{{ i.county_name }}
								{{ i.address }}
							</view>
							<!-- <text class="iconfont icon-bianji"  @click.stop="toDetail2(i)"></text> -->
							<view @click.stop="toDetail2(i)" class="">
								<uni-icons type="compose" size="20"></uni-icons>
							</view>

						</view>
					</view>
				</l-swipeAction>
			</block>
		</mescroll-body>
		<view class="fixed-btn">
			<view class="btn-cls flex" @click="addNewAddress">添加新地址</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				options: [{
						name: '设置默认',
						width: 150,
						background: '#f7f7f5',
						color: '#000'
					},
					{
						name: '删除',
						width: 150,
						background: '#ff395c',
						color: '#fff'
					}
				],
				list: [],
				token: uni.getStorageSync('login').token,
				isMine: '' //=1，代表是从‘我的’页面进入。否则是订单页进入。需要点击返回上一页
			};
		},
		onLoad(options) {
			this.isMine = options.isMine;

		},
		onShow() {
			this.mescroll?.resetUpScroll();
		},
		methods: {
			// 地址管理列表
			async upCallback(scroll) {
				const result = await this.$http.post({
					url: this.$api.addressList,
					data: {
						page: scroll.num,
						limit: 10,
					},
					headers: {
						'Authorization': `Bearer ${this.token}`
					}

				})
				// if (result.code == 200) {
				// 	console.log('地址管理列表', result);
				// 	this.mescroll.endByPage(result.data..length, this.$sun.pagingSum(result.data.count, 10));
				// 	if (scroll.num == 1) this.list = [];
				// 	this.list = this.list.concat(result.data.list);
				// 	console.log('this.list', this.list, this.list.length);
				// }
				if (result.code == 0) {
					console.log('地址管理列表', result);
					this.mescroll.endByPage(result.data.length, this.$sun.pagingSum(10, 10));
					if (scroll.num == 1) this.list = [];
					this.list = this.list.concat(result.data);
					console.log('this.list', this.list, this.list.length);
				}
			},
			// 添加地址
			addNewAddress() {
				console.log("添加地址===========");

				this.$sun.navGo('/pages/addressManagement/addressDetail');
			},
			// 编辑地址2
			toDetail2(items) {
				console.log(' 编辑地址2')
				this.$sun.navGo('/pages/addressManagement/addressDetail', {
					...items
				});
			},
			// 编辑地址
			toDetail(item) {
				this.defaultAddress(item.id, 1);
				setTimeout(()=>{
					this.$sun.navBack()
				},100)
				// console.log(item)

				// 我的页面，跳转详情页
				// if (this.isMine == 1) {
				// 	this.$sun.navGo('/pages/addressManagement/addressDetail', {

				// 	});
				// }
				// // 订单页进入——存缓存，并返回上一页
				// else {
				// 	// uni.setStorageSync('addressItem', item); //地址内容
				// 	// this.$sun.navBack();
				// 	this.defaultAddress(item.id, 2);
				// }
			},
			// 右滑'设为默认/删除' 事件
			clickItem(e, id) {
				console.log(e, id);
				let select = e.index;
				// 0:设为默认  1:删除
				if (select == 0) {
					this.defaultAddress(id, 1);
				}
				if (select == 1) {
					this.deleteAddress(id);
				}
			},

			// 设为默认
			async defaultAddress(id, type) {

				const result = await this.$http.post({
					url: this.$api.setDefault,
					data: {
						id: id,
					},
					headers: {
						'Authorization': `Bearer ${this.token}`
					}

				})
				if (result.code == 0) {
					this.mescroll?.resetUpScroll();
					this.$sun.toast(result.msg, 'none')
				} else {
					this.$sun.toast(result.msg, 'none')
				}
			},

			// 删除地址
			async deleteAddress(id) {
				const result = await this.$http.post({
					url: this.$api.delAddress,
					data: {
						id: id,
					},
					headers: {
						'Authorization': `Bearer ${this.token}`
					}

				})
				if (result.code == 0) {
					this.mescroll?.resetUpScroll();
					this.$sun.toast(result.msg, 'none')
				} else {
					this.$sun.toast(result.msg, 'none')
				}

			}
		}
	};
</script>

<style lang="less">
	.address-management-page {
		padding-bottom: 180rpx;

		.address-card {
			padding: 30rpx;
			border-bottom: 2rpx solid #f7f7f7;

			.v-default,
			.v-label {
				display: inline-block;
				padding: 4rpx 10rpx;
				border-radius: 4rpx;
				margin-left: 16rpx;
			}

			.v-default {
				background-color: #ffeff3;
				color: #da4369;
			}

			.v-label {
				background-color: #f4f6ff;
				color: #799ce8;
			}

			.address-bottom {
				view {
					margin: 0 15rpx;
					color: #929292;
				}

				text {
					font-size: 28rpx;
					color: #666;
					padding: 0 16rpx;
				}
			}
		}
	}
</style>